<?php 
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
class Live extends ActiveRecord
{
    public static function tableName()
    {
        return 'live';
    }
    public function rules()
        {
            return [
                [['live_title', 'live_content'], 'required']];
        }
    public static function getLive($title)
    {
       
        $live = Live::find()
         ->where(['live_title' => $title])
         ->one();
         return $live;
    }
    // 获得前几篇新编辑的文章，$num为获得的数量
    public static function getNewLive($num)
    {
          return Live::find()->limit($num)->orderBy('id desc')->all();

    }
    /* 
    和index的翻页有关，pageSize为页面最多存在的文章数，orderBy('id desc')为文章的排序。需要返回两个变量 $pagination和$articles
    */
    public static function getPaginationLive($pagesize)
    {
        $query = Live::find();
        $count = $query->count();
       $pagination = new Pagination(['totalCount' => $count,'pageSize' => $pagesize]);
       $lives = $query->offset($pagination->offset)
        ->limit($pagination->limit)
            ->orderBy('id desc')
        ->all();
        $paginationLive['pagination']= $pagination;
        $paginationLive['lives']= $lives;
        return $paginationLive;
    }
    public static function getLiveByPostname($Postname)
    {
        $post_live = Live::find()
        ->where(['post_name' => $Postname])
        ->one();
        return $post_live;
    }
    public static function getLiveById($id)
    {
        $live = static::find()->where(['id'=>$id])->one();
        return $live;
    }
}

 ?>